package project.taobao.commons.activemq.consumer.imple;

import org.springframework.jms.core.JmsTemplate;
import project.taobao.commons.activemq.consumer.Consumer;
import project.taobao.commons.activemq.utils.ActiveMqUtils;

import javax.jms.Destination;


public class JmsTemplateConsumer implements Consumer {
    private Object data;

    // methods
    @Override
    public Object consumerMessage(Destination queue) {
        JmsTemplate jmsTemplate = ActiveMqUtils.getJmsTemplate();
        try{
            data = jmsTemplate.receiveAndConvert(queue);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object getInfomation(){
        Object dataG = null;
        int i = 0;
        try{
            while(getData() == null){
                Thread.currentThread().sleep(10);
                if(i++ == 100){
                    throw new Exception("超时未得到信息");
                }
            }
            dataG = getData();
            setData(null);
            return dataG;
        } catch (Exception e){

        }
        return null;
    }
    // getters, setters
    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    // constructors
}
